package org.springblade.modules.repair.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.config.ProjectApiConfiguration;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Sharry
 * @createTime: 2025-07-28 19:00
 * @version: Version-1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/repair/partner/cloud")
@Api(value = "查询伙伴云", tags = "查询伙伴云")
public class PartnerCloudController {
	private final ProjectApiConfiguration projectApiConfiguration;

	@GetMapping("/search/order/main")
	@ApiOperation("根据订单号单独查询伙伴云订单主表信息")
	@ApiOperationSupport(order = 1)
	public R<JSONArray> searchOrderMainByOrderId(String orderId) {
		String body = HttpUtil
			.createGet(projectApiConfiguration.getWeb() + "/erp/cloud/search/order/main" + "?orderId=" + orderId)
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.execute(false)
			.body();
		JSONObject jsonObject = JSONObject.parseObject(body, JSONObject.class);
		if(Func.isNotEmpty(jsonObject)){
			JSONArray data = jsonObject.getJSONArray("data");
			return R.data(data);
		}
		return R.data(null);
	}

	@GetMapping("/search/order/sub")
	@ApiOperation("根据订单号单独查询伙伴云发货单表信息")
	@ApiOperationSupport(order = 1)
	public R<JSONArray> searchOrderSubByOrderId(String orderId) {
		String body = HttpUtil
			.createGet(projectApiConfiguration.getWeb() + "/erp/cloud/search/order/sub" + "?orderId=" + orderId)
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.execute(false)
			.body();
		// 将body转换为JSONObject
		JSONObject jsonObject = JSONObject.parseObject(body, JSONObject.class);
		if(Func.isNotEmpty(jsonObject)){
			JSONArray data = jsonObject.getJSONArray("data");
			return R.data(data);
		}
		return R.data(null);
	}

	@GetMapping("/search/delivery/detail")
	@ApiOperation("根据订单号单独查询伙伴云订单发货明细表信息")
	@ApiOperationSupport(order = 3)
	public R<JSONArray> searchDeliveryDetailByDeliveryId(String deliveryNote) {
		String body = HttpUtil
			.createGet(projectApiConfiguration.getWeb() + "/erp/cloud/search/delivery/detail" + "?deliveryNote=" + deliveryNote)
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.execute(false)
			.body();
		JSONObject jsonObject = JSONObject.parseObject(body, JSONObject.class);
		if(Func.isNotEmpty(jsonObject)){
			JSONArray data = jsonObject.getJSONArray("data");
			return R.data(data);
		}
		return R.data(null);
	}

	@GetMapping("/search/order")
	@ApiOperation("根据订单号单独查询伙伴云订单信息")
	@ApiOperationSupport(order = 3)
	public R<JSONArray> searchOrderByOrderId(String orderId) {
		String body = HttpUtil
			.createGet(projectApiConfiguration.getWeb() + "/erp/cloud/search/order" + "?orderId=" + orderId)
			.header(TokenConstant.HEADER, AuthUtil.getHeader(WebUtil.getRequest()))
			.execute(false)
			.body();
		JSONObject jsonObject = JSONObject.parseObject(body, JSONObject.class);
		if(Func.isNotEmpty(jsonObject)){
			JSONArray data = jsonObject.getJSONArray("data");
			return R.data(data);
		}
		return R.data(null);
	}



}
